package com.loan.controller.app.channel;

import cn.hutool.core.util.ObjectUtil;
import com.loan.common.annotation.Log;
import com.loan.common.core.controller.BaseController;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.domain.Result;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.enums.BusinessType;
import com.loan.system.domain.ChannelEntity;
import com.loan.system.service.ChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Api(value = "渠道管理", tags = {"渠道管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/app/channel")
public class ChannelController extends BaseController {

    private final ChannelService channelService;

    /**
     * 获取渠道列表
     */
    @ApiOperation("获取渠道列表（分页）")
    @GetMapping("/page")
    public TableDataInfo<ChannelEntity> list(ChannelEntity entity, PageQuery pageQuery) {
        return channelService.selectPageList(entity, pageQuery);
    }

    @ApiOperation("获取渠道列表")
    @GetMapping("/list")
    public Result<List<ChannelEntity>> getList() {
        return Result.ok(channelService.getList());
    }

    /**
     * 根据id查询详情
     */
    @ApiOperation("根据id查询详情")
    @GetMapping(value = {"/{id}"})
    public Result<ChannelEntity> getInfo(@ApiParam("ID") @PathVariable(value = "id", required = false) Long id) {
        if (ObjectUtil.isNotNull(id)) {
            return Result.ok(channelService.selectChannelById(id));
        }
        return Result.ok();
    }

    /**
     * 新增渠道
     */
    @ApiOperation("新增渠道")
    @Log(title = "新增渠道", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody ChannelEntity entity) {
        return toAjax(channelService.addChannel(entity));
    }

    /**
     * 修改渠道
     */
    @ApiOperation("修改渠道")
    @Log(title = "修改渠道", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Void> update(@Validated @RequestBody ChannelEntity entity) {
        return toAjax(channelService.updateChannel(entity));
    }

    /**
     * 删除渠道
     */
    @ApiOperation("删除渠道")
    @Log(title = "删除渠道", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result<Void> remove(@ApiParam("ID") @PathVariable("id") Long id) {
        return toAjax(channelService.deleteChannelById(id));
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    @Log(title = "状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public Result<Void> changeStatus(@RequestBody ChannelEntity entity) {
        return toAjax(channelService.updateStatus(entity));
    }
}
